<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>登录到数据库</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.command.html">MongoDB::command</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongodb.authenticate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB::authenticate</h1>
  <p class="verinfo">(PECL mongo &gt;=1.0.1)</p><p class="refpurpose"><span class="refname">MongoDB::authenticate</span> &mdash; <span class="dc-title">登录到数据库</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-mongodb.authenticate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>MongoDB::authenticate</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
   )</div>

  <p class="para rdfs-comment">
   此方法能够认证它的连接。
   如果数据库服务器开启了认证（默认没有开启），在做任何操作之前你需要登录。
  </p>
  <p class="para">
   通常情况下，你应该使用 <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span> 内置的参数而不是此方法。
   如果你对一个连接验证了，然后在会话期间连接掉了然后重连，你会被重新验证。
   如果你用这个方法手动验证，然后连接掉了，你必须在重新连接时手动调用这个方法。
  </p>
  <p class="para">
   该方法等同于运行：
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$salted&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:mongo:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hash&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">$salted</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$nonce&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"getnonce"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$saltedHash&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">$nonce</span><span style="color: #007700">[</span><span style="color: #DD0000">"nonce"</span><span style="color: #007700">].</span><span style="color: #DD0000">"</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}${</span><span style="color: #0000BB">hash</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"authenticate"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"user"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"nonce"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$nonce</span><span style="color: #007700">[</span><span style="color: #DD0000">"nonce"</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"key"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$saltedHash<br /></span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p>
  <p class="para">
   当一个连接验证后，它仅能够通过 &quot;logout&quot; 数据库命令来登出：
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"logout"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-mongodb.authenticate-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">username</code></em>
     </dt>

     <dd>

      <p class="para">
       用户名。
      </p>
     </dd>

    
    
     <dt>

      <em><code class="parameter">password</code></em>
     </dt>

     <dd>

      <p class="para">
       密码（明文格式）。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-mongodb.authenticate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回数据库的响应，如果登录成功，它会返回
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

   如果出现了什么错误，它会返回
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"errmsg"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"auth&nbsp;fails"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  （&quot;auth fails&quot; 可能是另外的信息，取决于数据库版本和发生了什么错误）。
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongodb.authenticate-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   MongoDB 关于 <a href="http://docs.mongodb.org/manual/reference/command/authenticate/" class="link external">&raquo;&nbsp;authenticate</a> 的核心文档。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb.authenticate-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>1.2.11</td>
       <td>
        使用时产生 <strong><code>E_DEPRECATED</code></strong>。
        请将验证细节传入到构造器。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.command.html">MongoDB::command</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
